import { createAxios, defHttp } from '@/utils/http/axios';
import { AxiosProgressEvent } from 'axios';
import { UploadApiResult } from '@/api/sys/model/uploadModel';
import { ResultPage, UploadFileParams } from '#/axios';
import { getToken } from '@/utils/auth';

enum Api {
  deviceDetail = '/tbuser/devices/deviceDetail/',
  getDeviceBySn = '/tbuser/devices/getDevice',
  updateProductId = '/tbuser/devices/updateProductId',
  selectList = '/tbuser/devices/selectList',
  excel2AddJson = '/tbuser/devices/checkDevicesBatchImport',
  removeDevice = '/tbuser/devices/removeDevice',
  exportAllDevice = '/tbuser/devices/exportByTemplate',
  activateDevice = '/tbuser/devices/v1/activateDevice/',
}

export function exportAllDeviceApi(data: { startTs: number; endTs: number }) {
  const token = getToken();
  fetch('/hc-api' + Api.exportAllDevice, {
    method: 'POST',
    body: JSON.stringify({
      beginTime: data.startTs, //开始时间
      endTime: data.endTs, //结束时间
    }),
    headers: [
      ['ContentType', 'application/json;charset=UTF-8'],
      ['Authorization', token],
    ],
  })
    .then(function (response: Response) {
      return response.blob();
    })
    .then(function (blob) {
      const link = document.createElement('a');
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      document.body.appendChild(link);
      link.click();
      // 释放的 URL 对象以及移除 a 标签
      URL.revokeObjectURL(link.href);
      document.body.removeChild(link);
    });
}

export function updateDeviceProductIdApi(data: { sn: string; productId: string }) {
  return createAxios({
    requestOptions: {
      apiUrl: '/hc-api',
    },
  }).post({
    url: Api.updateProductId,
    data,
  });
}

export function activateDeviceApi(data: { deviceId: string | number }) {
  return createAxios({
    requestOptions: {
      apiUrl: '/hc-api',
    },
  }).get({
    url: Api.activateDevice + data.deviceId,
  });
}

export function removeDeviceApi(data: { sn: string }) {
  return createAxios({
    requestOptions: {
      apiUrl: '/hc-api',
    },
  }).post({
    url: Api.removeDevice,
    data,
  });
}

export function getDeviceBySnApi(sn: string): Promise<Devices> {
  return createAxios({
    requestOptions: {
      apiUrl: '/hc-api',
    },
  }).post({
    url: Api.getDeviceBySn,
    data: {
      sn: sn,
    },
  });
}

export enum DeviceStatusEnum {
  ONLINE = 'ONLINE',
  OFFLINE = 'OFFLINE',
}

export enum DeviceSystemUpgradeStatusEnum {
  UPGRADE = 'UPGRADE',
  NORMAL = 'NORMAL',
}

export enum DevicePositioningTypeEnum {}
// Enum values here

export interface Devices {
  // Properties here
  serverId: number;
  deviceName: string;
  deviceSecret: string;
  deviceModel: string;
  imei: string;
  sn: string;
  // more properties...
}
export function selectDeviceListApi(data: {
  pageNum: number;
  pageSize: number;
  snStr: string;
}): Promise<ResultPage<Devices>> {
  return createAxios({
    requestOptions: {
      apiUrl: '/hc-api',
    },
  }).get({
    url: Api.selectList,
    params: data,
  });
}

export function deviceDetailApi(deviceId: String) {
  return createAxios({
    // headers: {
    //   Authorization:
    //     'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl90b2tlbl9rZXkiOiJsb2dpbl90b2tlbnM6YXBwX2JhY2tncm91bmQ6MTo3ZWNiMWVlOC0zODJkLTRmZmEtOWVkMC03ODBjNzdmMjQ3ZjQifQ.rwQ7J-xqVYMHooJznJD76F1BTCcHH2b-sKABFweTLulLRu5Lukfsw6yM7yBR0Vbvheq_wDUkVVo266ieuXGJCg',
    // },
    requestOptions: {
      apiUrl: '/hc-api',
      // withToken: false,
    },
  }).get(
    {
      url: Api.deviceDetail + deviceId,
    },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}

// export interface Excel2AddJsonReq {
//   // file name
//   file: File | Blob;

//   [key: string]: any;
// }

/**
 * @description: Upload interface
 */
export function excel2AddJson(
  url: string,
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: '/hc-api' + Api.excel2AddJson,
      onUploadProgress,
    },
    params,
  );
}
